package com.hotel.service.impl;

import com.hotel.comm.bean.DubboReply;
import com.hotel.comm.bean.IReply;
import com.hotel.comm.bean.person.EjobUser;
import com.hotel.comm.service.person.IPersonManagerService;
import com.hotel.Constants;
import com.hotel.person.repository.IEjobUserRepository;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class PersonManagerServiceImpl implements IPersonManagerService {
    private final Logger logger = LoggerFactory.getLogger(PersonManagerServiceImpl.class);

    @Autowired
    private IEjobUserRepository ejobUserRepository;

    @SuppressWarnings("rawtypes")
    @Override
    public IReply login(String personId, String password, String verifyCode) {
        try {
            if (logger.isDebugEnabled()) {
                logger.debug("personId=" + personId + ";password=" + password + ";verifyCode=" + verifyCode);
            }
            if (StringUtils.isBlank(personId)) {
                return new DubboReply(Constants.REPLY_CODE_LOGIN_LOGINIDNULL, Constants.REPLY_MSG_LOGIN_LOGINIDNULL);
            }
            EjobUser user = this.ejobUserRepository.getByLoginName(personId);
            if (user == null) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Can not Get Ejob User By Login Name!");
                }
                return new DubboReply(Constants.REPLY_CODE_LOGIN_LOGINIDNOTEXIST, Constants.REPLY_MSG_LOGIN_LOGINIDNOTEXIST);
            }
            logger.info(user.toString());
            if (logger.isDebugEnabled()) {
                logger.debug(user.toString());
            }
            if (StringUtils.equals(password, user.getPassword())) {
                user.setLastLoginDate(new Date());
                this.ejobUserRepository.saveAndFlush(user);
                return new DubboReply();
            }
            return new DubboReply(Constants.REPLY_CODE_LOGIN_PASSWORDWRONG, Constants.REPLY_MSG_LOGIN_PASSWORDWRONG);
        } catch (Exception e) {
            e.printStackTrace();
            return new DubboReply(Constants.REPLY_CODE_LOGIN_EXCEPTION, Constants.REPLY_MSG_LOGIN_EXCEPTION);
        }
    }

    public void updateEjobUser() throws Exception {
        throw new Exception("test");
    }

    @SuppressWarnings("rawtypes")
    @Override
    public IReply regist(String loginName, String password) {
        if (StringUtils.isBlank(loginName)) {
            return new DubboReply(Constants.REPLY_CODE_REGIST_REGISTLOGINNAMENULL, Constants.REPLY_MSG_REGIST_REGISTLOGINNAMENULL);
        }
        EjobUser userInDB = this.ejobUserRepository.getByLoginName(loginName);
        if (userInDB != null) {
            return new DubboReply(Constants.REPLY_CODE_REGIST_REGISTLOGINNAMEEXISTS, Constants.REPLY_MSG_REGIST_REGISTLOGINNAMEEXISTS);
        }
        if (StringUtils.isBlank(password)) {
            return new DubboReply(Constants.REPLY_CODE_REGIST_REGISTPASSWORDNULL, Constants.REPLY_MSG_REGIST_REGISTPASSWORDNULL);
        }
        EjobUser user = new EjobUser(loginName, password);
        this.ejobUserRepository.save(user);
        return new DubboReply();
    }

    @Override
    public IReply findUserByLoginName(String loginName) {
        try {
            EjobUser ejobUser = this.ejobUserRepository.getByLoginName(loginName);
            return new DubboReply(ejobUser);
        } catch (Exception e) {
            return new DubboReply(Constants.REPLY_CODE_FIND_USER_BY_NAME, Constants.REPLY_MSG_FIND_USER_BY_NAME);
        }
    }

}
